package com.dayaokeji.rhythmschoolstudent.client.home.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bigkoo.convenientbanner.holder.Holder
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.remoteImageUrl
import com.dayaokeji.rhythmschoolstudent.client.common.ad.AdCallback
import com.dayaokeji.rhythmschoolstudent.client.common.ad.AdViewCallback
import com.dayaokeji.rhythmschoolstudent.client.common.ad.bean.FileInfoWrapper
import com.dayaokeji.rhythmschoolstudent.glide.GlideUrlModel
import com.dayaokeji.rhythmschoolstudent.glide.GlideUtils
import com.dayaokeji.server_api.domain.FileInfo
import com.orhanobut.logger.Logger
import com.qq.e.ads.nativ.NativeExpressADView

/**
 *********************************************************************
 **  Created by Zeno on 2018/7/17
 *********************************************************************
 **  version 1.0
 *********************************************************************
 **  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 *********************************************************************
 **
 *********************************************************************
 */
class BannerAdapter(itemView: View?,val datas: List<FileInfoWrapper>,val cxt: Context?) : Holder<FileInfoWrapper>(itemView) {

    private var imageView: ImageView? = null
    private var contentView: View? = null;
    private var index: Int = 0;
    private var bannerSize:Int = datas.size

    companion object {
        private  var adView: NativeExpressADView? = null
    }


    override fun updateUI(data: FileInfoWrapper?) {
//        data?.adView?.destroy()
        data?.let {info ->
            if (info.id != null && info.adView == null) {
                itemView?.let {
                    if (imageView != null) {
                        Logger.d("banner download image url === "+remoteImageUrl+info.id)
                        GlideUtils.with(it.context,false).load(GlideUrlModel(remoteImageUrl+info.id)).into(imageView!!)
                    }
                }
            }else{
//                if (BannerAdapter.adView!=null){
//                    contentView?.let {
//                        if (it is ViewGroup) {
//                            it.removeView(BannerAdapter.adView)
//                        }
//                    }
//                }

                imageView?.visibility = View.GONE
                contentView?.let {itemView ->
                    if (itemView is ViewGroup) {
                        if (info.adView != null) {
                            val tag = itemView.findViewWithTag<NativeExpressADView>("Home_AD")
//                            if (tag == null) {
//                                itemView.addView(info.adView)
//                            }
                        }
                    }
                }
            }
        }
    }

    override fun initView(itemView: View?) {
        contentView = itemView;
        imageView = itemView?.findViewById<ImageView>(R.id.iv_cover)
    }

    fun onDestroyView() {
        adView?.destroy()
    }
}